﻿Imports DotNetNuke.Web.Services
Imports System.Web.Mvc
Imports DotNetNuke.Common
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Content.Common
Imports DotNetNuke.Entities.Host
Imports DotNetNuke.Entities.Users
Imports DotNetNuke.Security
Imports DotNetNuke.Services.Localization
Imports System.Text
Imports System.Web.HttpContext
Namespace DotNetNuke.Modules.ActiveForums
    Public Class AdminServiceController
        Inherits DnnController


        <DnnAuthorize()> _
        Public Function ToggleURLHandler(ModuleId As Integer) As String
            Dim objModules As New Entities.Modules.ModuleController
            Dim objSettings As New SettingsInfo
            objSettings.MainSettings = objModules.GetModuleSettings(ModuleId)
            Dim cfg As New ConfigUtils
            Dim success As Boolean = False
            If Utilities.IsRewriteLoaded() Then
                success = cfg.DisableRewriter(Server.MapPath("~/web.config"))
                Return "disabled"
            Else
                success = cfg.EnableRewriter(Server.MapPath("~/web.config"))
                Return "enabled"
            End If

        End Function


        <DnnAuthorize()> _
        Public Function GetSecurityGrid(GroupId As Integer, ForumId As Integer) As String
            Dim sb As New StringBuilder()



            Return sb.ToString()
        End Function

        <DnnAuthorize()> _
        <AcceptVerbs(HttpVerbs.Post)> _
        Public Function ToggleSecurity(ModuleId As Integer, Action As String, PermissionsId As Integer, SecurityId As String, SecurityType As Integer, SecurityKey As String, ReturnId As String) As String
            Dim db As New Data.Common
            Dim sb As New StringBuilder()
            Select Case Action
                Case "delete"
                    Permissions.RemoveObjectFromAll(SecurityId, SecurityType, PermissionsId)
                    Return String.Empty
                Case "addobject"
                    If SecurityType = 1 Then
                        Dim uc As New UserController
                        Dim ui As User = uc.GetUser(PortalSettings.PortalId, ModuleId, SecurityId)
                        If Not ui Is Nothing Then
                            SecurityId = ui.UserId.ToString
                        Else
                            SecurityId = String.Empty
                        End If
                    Else
                        If SecurityId.Contains(":") Then
                            SecurityType = 2
                        End If
                    End If
                    If Not String.IsNullOrEmpty(SecurityId) Then
                        Dim permSet As String = db.GetPermSet(PermissionsId, "View")
                        permSet = Permissions.AddPermToSet(SecurityId, SecurityType, permSet)
                        db.SavePermSet(PermissionsId, "View", permSet)
                    End If
                    Return String.Empty
                Case Else
                    Dim permSet As String = db.GetPermSet(PermissionsId, SecurityKey)
                    If Action = "remove" Then
                        permSet = Permissions.RemovePermFromSet(SecurityId, SecurityType, permSet)
                    Else
                        permSet = Permissions.AddPermToSet(SecurityId, SecurityType, permSet)
                    End If
                    db.SavePermSet(PermissionsId, SecurityKey, permSet)
                    Return Action & "|" & ReturnId.ToString()
            End Select



        End Function

    End Class
End Namespace
